﻿<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Table</title>
    <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="/css/global.css" media="all">
    <link rel="stylesheet" href="/plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/css/table.css"/>
    <style type="text/css">
    .searcheBottom{margin-bottom: 10px;}
    </style>
</head>

<body>
<div class="admin-main">
    <blockquote class="layui-elem-quote">
        <div class="demoTable">
        <div class="layui-inline searcheBottom">
            <a data-opt="add" href="/manage/navigation/addNavigation" class="layui-btn">
                <i class="layui-icon">&#xe608;</i>添加根目录菜单</a>
                </div>
        </div>
    </blockquote>
            <span style="font-size: 16px;"><b>菜单目录列表</b></span>
                <table class="layui-hide" id="tableID" lay-filter="tableFilter"></table>
</div>

<template id="editBoxTemplate" hidden>
    <div style="margin: 15px;">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>菜单编辑(<label class="layui-required-tip">标红的为必填项</label>)</legend>
        </fieldset>

        <form id="check-edit-form" class="layui-form js-form" action="/manage/video/publish" method="post">
            <input type="hidden" name="id" class="layui-input js-id" />
            <div class="layui-form-item">
                <label class="layui-form-label layui-required-tip">目录标题:</label>
                <div class="layui-input-block">
                    <input type="text" id="FORM_TITLE" name="title" lay-verify="required" autocomplete="off" placeholder="目录标题" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label layui-required-tip">图标:</label>
                <div class="layui-input-block">
                    <input type="text" id="FORM_ICON" name="icon" lay-verify="required" autocomplete="off" placeholder="请输入图标标识符" class="layui-input">
                    <code style="margin-top: 4px; display: block; color:#FF0000;">获取图标：
                        <a href="http://fontawesome.io/icons/" target="_blank">http://fontawesome.io/icons/</a>或者
                        <a href="http://www.layui.com/doc/element/icon.html" target="_blank">http://www.layui.com/doc/element/icon.html</a>
                    </code>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">URL:</label>
                <div class="layui-input-block">
                    <input type="text" id="FORM_URL" name="href" autocomplete="off" placeholder="URL" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label layui-required-tip">排序号:</label>
                <div class="layui-input-block">
                    <input type="text" id="FORM_RANK" name="rank" lay-verify="required" autocomplete="off" placeholder="排序号" class="layui-input">
                </div>
            </div>

            <button lay-filter="edit" lay-submit style="display: none;"></button>
        </form>
    </div>
    <script>
        layui.use(['form', 'layedit', 'laydate'], function() {
            var form = layui.form;
            form.render();
        });
    </script>

</template>

<script type="text/javascript" src="/plugins/layui/layui.js"></script>
<script>
    layui.config({
        base: '/js/'
    });

    layui.use(['form', 'laydate','table','laydate','util'], function () {
    	var $ = layui.jquery,
        form = layui.form,
        table = layui.table,//表格
        laydate = layui.laydate,//日期
        util = layui.util;//工具类
        pageInit();
            
      //执行一个 table 实例
        function pageInit(){
      	   table.render({
                 elem: '#tableID'
                 ,url:'/manage/navigation/rest/restList'
                 ,method: 'post'
                 ,cols: [[
                     {field:'id', title: 'ID',width:100, sort: true}
                     ,{field:'title', title: '目录',width:300}
                     ,{field:'icon', title: '图标',width:100,templet:function(d){
                    	 if(d.icon.indexOf('fa-') !== -1){
                    		 return '<div><i class="fa '+d.icon+'" ></i></div>';
                    	 }else{
                    		 return '<div><i class="layui-icon fa'+d.icon+'" >'+d.icon+'</i></div>';
                    	 }
                    	 
                     }}
                     ,{field:'href', title: 'URL'}
                     ,{field:'rank', title: '排序号'}
                     ,{title: '操作',  align:'left',width:350, templet:function(d){
   		 	    		  return '<a class="layui-btn layui-btn-xs" lay-event="edit">'
                              +'编辑</a>'
   		 	    		      +'<a href="/manage/navigation/childNavList?parentId='+d.id+'&parentName='+d.title+'" class="layui-btn layui-btn-normal layui-btn-xs">'
   		                      +'子菜单管理</a>'
   		                      +'<a href="/manage/navigation/addChildNavigation?parentId='+d.id+'" class="layui-btn  layui-btn-xs">'
   		                      +'添加子菜单</a>'
   		                      +'<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">'
   		                      +'删除</a>';
                     }}
                 ]]
                 ,page: true
             });
        }
            
        //监听工具条
        table.on('tool(tableFilter)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            var editBoxIndex = -1;
            if(layEvent === 'delete'){
            	var id = data.id;
                //配置一个透明的询问框
                layer.confirm('确定要删除吗?', {
                    btn : [ '确定', '取消' ]//按钮
                }, function(index) {
                    $.ajax({
                        cache: true,
                        type: "GET",
                        url: "/manage/navigation/rest/deleteNavByParentId",
                        data: "id="+id,
                        async: false,
                        error: function () {
                            layer.close(index);
                            alert("Connection error");
                        },
                        success: function (data) {
                            layer.close(index);
                            table.reload("tableID");
                        }
                    });
                });
            } else if(layEvent === 'edit'){
            	if (editBoxIndex !== -1) {
                    return;
                }
                var template = $('#editBoxTemplate').html();
                var ctx = $(this);
                editBoxIndex = layer.open({
                    type: 1,
                    title: '菜单编辑',
                    content: template,
                    btn: ['保存', '取消'],
                    area: ['800px', '80%'],
                    yes: function (index) {
                        //触发表单的提交事件
                        $('#layui-layer' + editBoxIndex).find('.js-form [lay-submit]').click();
                    },
                    full: function (elem) {
                        var win = window.top === window.self ? window : parent.window;
                        $(win).on('resize', function () {
                            var $this = $(this);
                            elem.width($this.width()).height($this.height()).css({
                                top: 0,
                                left: 0
                            });
                            elem.children('div.layui-layer-content').height($this.height() - 95);
                        });
                    },
                    success: function (layero, index) {
                        //弹出窗口成功后渲染表单
                        form.render();
                    },
                    end: function () {
                        editBoxIndex = -1;
                    }
                });

                layer.ready(function () {
                    var $layer = $('#layui-layer' + editBoxIndex);
                    //菜单ID
                    $layer.find('.js-id').val(data.id);
                    //标题
                    FORM_TITLE.value = data.title;
                    //设置图标
                    FORM_ICON.value = data.icon;
                    //设置URL
                    FORM_URL.value = data.href;
                    //设置排序号
                    FORM_RANK.value = data.rank;

                    form.on('submit(edit)', function (data) {
                        //这里可以写ajax方法提交表单
                        $.ajax({
                            cache: false,
                            type: "POST",
                            url: "/manage/navigation/rest/editNavigation",
                            data: $("#check-edit-form").serialize(),
                            async: true,
                            error: function () {
                                alert("Connection error");
                            },
                            success: function (msg) {
                                table.reload("tableID");
                                layer.closeAll();
                            }
                        });
                        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                    });
                })
            }
        });
    });
</script>
</body>

</html>